var columns = [[
                {field:'id',title:'ID',width:50},
                {field:'name',title:'分类名称',width:100},
                {field:'code',title:'分类编号',width:100},
                {field:'status',title:'状态',width:100,formatter: function (value, row, index) {
                    switch(value) {
                        case 0 : return '删除';
                        case 1 : return '正常';
                    }
                    return '未知';
                }},
                {field:' ', title:'操作',width:100,align:'center',formatter: function(value,row,index){
            	    var up = "<a style='color:blue' onclick='up("+row["id"]+")'>上移</a>";
            	    var down = "<a style='color:blue' href='#' title=\"下移\" href='#' onclick='down("+row["id"]+")'>下移</a>";
            	    var modify = "<a style='color:blue' href='#' onclick='modify("+row["id"]+",\""+row["name"]+"\""+",\""+row["code"]+"\")'>修改</a>";
            	    var del = "<a style='color:blue' href='#' onclick='del("+row["id"]+")'>删除</a>";
            	    
            	    if(row["id"]==7){
            	    	return "";
            	    }
            	    if(row["status"]==0){
            	    	//return up + "&nbsp;" + down + "&nbsp;" + modify;
            	    	return modify;
            	    }
                    //return up + "&nbsp;"+ down + "&nbsp;"+ modify + "&nbsp;" + del;
                    return modify + "&nbsp;" + del;
                }}
			]];

$(document).ready(function () {

	$('#ff').form( {
		success : function(data) {
			data = eval('(' + data + ')');
			var iHeight = document.documentElement.clientHeight;
            $('#tt').datagrid( {
                width : '100%',
                height : iHeight-110,
                fitColumns : true,
                striped:true,
                columns : columns,
                singleSelect:true,// 是否单选
                toolbar: CreateToolBar(),
                pagination:true,
                pageList: [20,30,50,100],
                data:data.slice(0,20)
            });
            
            var pager = $("#tt").datagrid("getPager");  
            pager.pagination({  
                total:data.length,  
                onSelectPage:function (pageNo, pageSize) {  
                    var start = (pageNo - 1) * pageSize;  
                    var end = start + pageSize;  
                    $("#tt").datagrid("loadData", data.slice(start, end));  
                    pager.pagination('refresh', {  
                        total:data.length,  
                        pageNumber:pageNo  
                    });  
                }  
            });  
            
			try{window.parent.unmask();}catch(err){}
		}
	});
	$('#fmAdd').form( {
		success : function(data) {
		    CRUD.queryList();
		    try{
			    var result = eval('(' + data + ')');
			    if(result == true){
			    	$('#dialogAdd').dialog('close');
				 } else {
			        $.messager.alert('提示', '保存失败');
				 }
			}catch(err){
			}
		}
	});

    CRUD.queryList();
    InitForm();

    // 确定添加
    $('#a_AddOK').click(function () {
        if (!document.getElementById("name").value) {
            $.messager.alert('提示', '分类名称不能为空！');
            return;
        }
        $('#fmAdd').submit();
    });

    // 取消
    $('#a_AddCancle').click(function () {
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#fmAdd').form('clear');
        $('#dialogAdd').dialog('close');
        $('#tt').datagrid("clearSelections");
    });
});

var CRUD = {
    queryList: function () {
       try{window.parent.mask();}catch(err){}
	   $('#ff').submit();
    },

    Add: function () {
    	$('#fmAdd').form('clear');
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#dialogAdd').dialog('open');
        $('#tt').datagrid("clearSelections");
        $('#dialogAdd').dialog("setTitle", "添加部门分类");
    },
    Del: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r) {
            $.messager.confirm('删除提示', '你确认删除这一条数据吗？', function (d) {
                if (d) {
                    $.get(getContextPath() + "/department/type/delete.do?id=" + r.id, function (data) {
                        if (data == 'true') {
                            $.messager.show(
                                    {
                                        title: '系统提示',
                                        msg: '删除成功！',
                                        timeout: 1500
                                    });
                            $('#tt').datagrid("clearSelections");
                            CRUD.queryList();
                        } else {
                            $.messager.alert('错误', '删除失败！');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请选择一行数据!');
        }
    },
    Update: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#dialogAdd').dialog('open');
        $('#fmAdd').form('load', r);
        $('#dialogAdd').dialog("setTitle", "修改部门分类");
    }
};

function modify(id, name) {

    $('#dialogAdd').dialog('open');
    $('#fmAdd').form('load',{
    		name: name,
    		id: id
    	});
    $('#dialogAdd').dialog("setTitle", "修改添加部门分类");
}
	
function del(id) {
	$.messager.confirm('删除该部门分类', '你确认删除该部门分类吗?', function (d) {
		if (d) {
			$.get(getContextPath() + "/department/type/delete.do?id=" + id, function (data) {
				if (data == 'true') {
					$.messager.show({
						title: '系统提示',
						msg: '删除成功！',
						timeout: 1500
					});
					$('#tt').datagrid("clearSelections");
					CRUD.queryList();
				} else {
					$.messager.alert('错误', '删除失败！');
				}
			});
		}
	});
}

function up(id) {
	$.get(getContextPath() + "/department/type/sort.do?id=" + id+"&aim=1", function (data) {
		if (data == 'true') {
			$.messager.show({
				title: '系统提示',
				msg: '排序成功！',
				timeout: 1500
			});
			$('#tt').datagrid("clearSelections");
			CRUD.queryList();
		} else {
			$.messager.alert('错误', '删除失败！');
		}
	});
}

function down(id) {
	$.get(getContextPath() + "/department/type/sort.do?id=" + id+"&aim=2", function (data) {
		if (data == 'true') {
			$.messager.show({
				title: '系统提示',
				msg: '排序成功！',
				timeout: 1500
			});
			$('#tt').datagrid("clearSelections");
			CRUD.queryList();
		} else {
			$.messager.alert('错误', '删除失败！');
		}
	});
}

function CreateToolBar() {
    var a = [];
    var isReflesh = true;
    var isAdd = true;
    var isDel = true;
    var isUpdate = true;
    if (isAdd) {
        a.push({
            text: '添加',
            iconCls: 'icon-add',
            handler: function () {
                CRUD.Add();
            }
        });
        a.push('-');
    }

    if (isUpdate) {
        a.push({
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                CRUD.Update();
            }
        });
        a.push('-');
    }

    if (isDel) {
        a.push({
            text: '删除',
            iconCls: 'icon-cut',
            handler: function () {
                CRUD.Del();
            }
        });
        a.push('-');
    }
    return a;
}

function InitForm() {
    var width = 500;
    var height = window.screen.height;
    if (height > 200) {
        height = 200;
    }
    $('#dialogAdd').dialog({
        closed: true,
        title: "添加部门分类",
        width: width,
        height: height
    });
    
}
